
from myelin.server.lib.http import ParamValidators

from Service import Service 


class Service(Service):
    
    def fetchParams(self, context, source, params, errors):
        config = context.event.data.config
        print "CONFIG PARAMS: ", config.params
        
        acct = context.account
        if acct.isAdmin:
            max = None
        elif acct.isAgent():
            max = config.params.get((None,'user-query-length')) or self.defaults.get('query-length')            
            if max: max = int(max)
        else:
            max = config.params.get((None,'guest-query-length')) or self.defaults.get('query-length')            
            if max: max = int(max)
            
        args = context.request.args
        
        #print "args: %s" % args
        params['query'] = ParamValidators.String(
            'query',
            args,
            errors,
            min=4,
            max=max,
            escape=0
        )
        
        # the protocol says cardinality of (0,1)
        # but, SPARQL Query says multiple FROM clauses allowed,
        # in which, RDF merge is applied
        params['default-graph-uri'] = ParamValidators.StringSplit(
            'default-graph-uri',
            args,
            errors,
            max=self.defaults.get('graph-uri-count'),
            escape=0
        )
        
        params['named-graph-uri'] = ParamValidators.StringSplit(
            'named-graph-uri',
            args,
            errors,
            max=self.defaults.get('graph-uri-count'),
            escape=0
        )
        
        params['graph-result-method'] = ParamValidators.StringSplit(
            'graph-result-method',
            args,
            errors,
            max=self.defaults.get('graph-result-method-length'),
            escape=0
        )
        
        params['nsmaps'] = ParamValidators.String(
            'nsmaps',
            args,
            errors,
            min=0,
            max=max,
            escape=0,
            encoding='iso-8859-1'
        )
        
        if not params['nsmaps'] and 'quick-search' in args:
            params['nsmaps'] = config.defaultNsMapString
        
        return params, errors
        